<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="root"></div>
    <script src="https://unpkg.com/react@18/umd/react.development.js" crossorigin></script>
    <script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js" crossorigin></script>
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
    <script type="text/babel">
        class Reg extends React.Component{
            constructor(props){
                super(props);
                this.state = {
                    username:{
                        info:"",
                        value:""
                    },
                    pwd:{
                        info:"",
                        value:""
                    },
                    confirm:{
                        info:"",
                        value:""
                    }
                }
            }
            validateUsername = (e) => {
                if(/^\w{6,20}$/.test(e.target.value)){
                    this.setState({
                        username:{
                            info:"√",
                            value:e.target.value
                        }
                    });
                }else{
                    this.setState({
                        username:{
                            info:"X",
                            value:e.target.value
                        }
                    });
                }
            }
            validatePwd = (e) => {
                if(/^.{6,20}$/.test(e.target.value)){
                    this.setState({
                        pwd:{
                            info:"√",
                            value:e.target.value
                        }
                    });
                }else{
                    this.setState({
                        pwd:{
                            info:"X",
                            value:e.target.value
                        }
                    });
                }
            }
            validateConfirm = (e) => {
                console.log(e.target.value,this.state.pwd.value);
                // console.log(this.refs.pwd.refs.ipt.value);
                if(e.target.value == this.state.pwd.value){
                    this.setState({
                        confirm:{
                            info:"√",
                            value:e.target.value
                        }
                    });
                }else{
                    this.setState({
                        confirm:{
                            info:"X",
                            value:e.target.value
                        }
                    });
                }

            }
            render(){
                return <div>
                    <h1>注册</h1>
                    <InputElement type="text"
                        validate={this.validateUsername}
                        info={this.state.username.info}
                    >用户名：</InputElement>
                    <InputElement type="password"
                        ref="pwd"
                        validate={this.validatePwd}
                        info={this.state.pwd.info}
                    >密码：</InputElement>
                    <InputElement type="password"
                        validate={this.validateConfirm}
                        info={this.state.confirm.info}
                    >确认密码：</InputElement>
                    <input type="submit" />
                </div>
            }
        }
        class InputElement extends React.Component{
            constructor(props){
                super(props);
            }
            render(){
                return <div>
                    <label>{this.props.children}</label>    
                    <input ref="ipt" type={this.props.type} onBlur={this.props.validate}/>
                    <span>{this.props.info}</span>
                </div>
            }
        }
        ReactDOM.createRoot(document.getElementById("root")).render(<Reg />);

    </script>
</body>
</html>